package com.lyc.common.interceptor;

import com.lyc.user.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author YooLin
 * @Date 2017/7/23 18:09
 * @Description session拦截器 没有session自动跳转登录界面
 */
public class SessionInterceptor implements HandlerInterceptor {
    /**
     * 校验登录拦截器
     * @param request
     * @param response
     * @param o
     * @return true 放行 将请求交给对应的Controller中的方法执行； false 作废该请求，不对该请求做任何处理
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
//        String uri = request.getRequestURI();
//        //如果请求地址有带有"login" 放行 以"/attend/signIn"结尾的请求地址 放行
//        if (uri.contains("login")||uri.endsWith("/attend/signIn")){
//            return true;
//        }
//        //如果session有用户信息 放行
//        if (request.getSession().getAttribute("userInfo") instanceof User) {
//            User user = (User) request.getSession().getAttribute("userInfo");
//            if (user!=null)
//                return true;
//        }
//        //用户没登录 请求转发到登录页面让用户登录
//        request.getRequestDispatcher("/login").forward(request,response);
//        return false;
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
